package megalopolis.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import megalopolis.common.ManagableThread;

/**
 * User: Kirill
 * Date: 19.02.2008
 */
public class ListenerThread extends ManagableThread {

	private NetworkThread handler;

	private ServerSocket serverSocket;

	private int port;

	public ListenerThread(int port, NetworkThread handler) {
		super(0);
		this.port = port;
		this.handler = handler;
	}

	protected void init() {
		try {
			serverSocket = new ServerSocket(port);
		} catch (IOException e) {
			throw new RuntimeException("Can't create server socket on port: " + port, e);
		}
	}

	public void threadStep() {
		try {
			Socket socket = serverSocket.accept();
			handler.addPlayer(socket);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
